<script setup lang='ts'>
  import { onMounted, reactive, ref, nextTick } from 'vue';
  import { equipApi } from '@/api/controller';
  import { Toastloading, ToastSuccess } from '@/utils/public';
  const dataForm = ref()
  const action = reactive({
    total:0,
    dialogFormVisible:false,
    disabled:false,
    list:[] as any[],
  })
  interface formQuert {
    id:number
    sort:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    sort: [{ required: true, trigger: 'blur', message: '请输入装备类型排序' }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      const loading = Toastloading()
      await equipApi.editEquipType(form).then(res=>{
        loading.close()
        action.dialogFormVisible = false
        getList()
        ToastSuccess('修改成功')
      }).catch(()=>{
        loading.close()
      })
    })
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  const handleTypeForm = (row:any = {})=>{
    const { id, sort } = row
    Object.assign(form,{
      id,
      sort
    })
    action.dialogFormVisible = true
  }
  const getList = async()=> {
    const loading = Toastloading()
    const res = await equipApi.getEquipType()
    loading.close()
    const { list=[] } = res.data
    action.list = list
  }
  onMounted(()=>{
    getList()
  })
</script>

<template>
  <div class="xm-content">
    <el-table
      class="xm-main xm-TableCenter"
      :data="action.list"
      border>
      <el-table-column min-width="120" prop="id" label="装备类型" />
      <el-table-column min-width="120" prop="sort" label="装备类型排序" />
      <el-table-column min-width="120" prop="name" label="装备类型名称" />
      <el-table-column width="180" fixed=right label="操作">
        <template #default="scope">
          <el-button type="primary" @click="handleTypeForm(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      top="8vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="50%"
      title="编辑装备类型">
        <el-form ref="dataForm" :model="form" :rules="formRules">
          <el-form-item label="装备类型排序" prop="sort">
            <el-input v-model="form.sort" type="number" placeholder="请输入装备类型排序" />
          </el-form-item>
        </el-form>
        <template v-if="!action.disabled" #footer>
          <span class="dialog-footer">
            <el-button @click="closeDialog">取 消</el-button>
            <el-button type="primary" @click="handleSubmit()">确 定</el-button>
          </span>
        </template>
      </el-dialog>
  </div>
</template>